package com.ruoyi.gcgl.service.impl;

import java.util.HashMap;
import java.util.List;

import javax.annotation.Resource;

import org.activiti.engine.IdentityService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.runtime.ProcessInstance;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ruoyi.common.core.text.Convert;
import com.ruoyi.gcgl.domain.YyProjectChange;
import com.ruoyi.gcgl.mapper.YyProjectChangeMapper;
import com.ruoyi.gcgl.service.IYyProjectChangeService;

/**
 * 项目施工变更单Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-08-07
 */
@Service
public class YyProjectChangeServiceImpl implements IYyProjectChangeService 
{
    @Autowired
    private YyProjectChangeMapper sjProjectChangeMapper;
    @Resource
    private RuntimeService runtimeService;
    @Resource
    private TaskService taskService;
    @Resource
    IdentityService identityService;

    /**
     * 查询项目施工变更单
     * 
     * @param id 项目施工变更单主键
     * @return 项目施工变更单
     */
    @Override
    public YyProjectChange selectSjProjectChangeById(Long id)
    {
        return sjProjectChangeMapper.selectSjProjectChangeById(id);
    }

    /**
     * 查询项目施工变更单列表
     * 
     * @param sjProjectChange 项目施工变更单
     * @return 项目施工变更单
     */
    @Override
    public List<YyProjectChange> selectSjProjectChangeList(YyProjectChange sjProjectChange)
    {
        return sjProjectChangeMapper.selectSjProjectChangeList(sjProjectChange);
    }

    /**
     * 新增项目施工变更单
     * 
     * @param sjProjectChange 项目施工变更单
     * @return 结果
     */
    @Override
    public int insertSjProjectChange(YyProjectChange sjProjectChange)
    {
    	int rows = sjProjectChangeMapper.insertSjProjectChange(sjProjectChange);
    	// 发起合同审核流程
        identityService.setAuthenticatedUserId(sjProjectChange.getOwner());
        HashMap<String, Object> variables = new HashMap<>();
        variables.put("applyuserid", sjProjectChange.getOwner());
        variables.put("level1", sjProjectChange.getSjfzr());
        ProcessInstance pi = runtimeService.startProcessInstanceByKey("yybiangengdan", String.valueOf(sjProjectChange.getId()), variables);
    	
        YyProjectChange obj = new YyProjectChange();
    	obj.setId(sjProjectChange.getId());
    	obj.setProcessInstanceId(pi.getProcessInstanceId());
    	sjProjectChangeMapper.updateSjProjectChange(obj);
    	
        return rows;
    }

    /**
     * 修改项目施工变更单
     * 
     * @param sjProjectChange 项目施工变更单
     * @return 结果
     */
    @Override
    public int updateSjProjectChange(YyProjectChange sjProjectChange)
    {
    	// 发起合同审核流程
//        identityService.setAuthenticatedUserId(sjProjectChange.getOwner());
//        HashMap<String, Object> variables = new HashMap<>();
//        variables.put("applyuserid", sjProjectChange.getOwner());
//        variables.put("level1", sjProjectChange.getSjfzr());
//        ProcessInstance pi = runtimeService.startProcessInstanceByKey("sjbiangengdan", String.valueOf(sjProjectChange.getId()), variables);
//        sjProjectChange.setProcessInstanceId(pi.getProcessInstanceId());
    	
        return sjProjectChangeMapper.updateSjProjectChange(sjProjectChange);
    }

    /**
     * 批量删除项目施工变更单
     * 
     * @param ids 需要删除的项目施工变更单主键
     * @return 结果
     */
    @Override
    public int deleteSjProjectChangeByIds(String ids)
    {
        return sjProjectChangeMapper.deleteSjProjectChangeByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除项目施工变更单信息
     * 
     * @param id 项目施工变更单主键
     * @return 结果
     */
    @Override
    public int deleteSjProjectChangeById(Long id)
    {
        return sjProjectChangeMapper.deleteSjProjectChangeById(id);
    }

	@Override
	public int updateSjProjectChange4check(YyProjectChange sjProjectChange) {
		// TODO Auto-generated method stub
		return sjProjectChangeMapper.updateSjProjectChange(sjProjectChange);
	}
}
